####
#### Eroll Kuhn & Rahsaan Maxwell
#### Generating P-value plots (Balance checks)


rm(list=ls())

# read in frame
df <- read.csv(".../2.Balance Checks/Output/B.Tables/Combined_Table.csv")


# pre-processing
df <- data.frame(df)

df$facet_lab <- ifelse(df$Outcome=="% Foreign-Born",1,
                       ifelse(df$Outcome=="% Non EU",2,
                              ifelse(df$Outcome=="% from Arabic-Speaking Countries",3,
                                     ifelse(df$Outcome=="% Co-National",4,NA))))



df$facet_lab <- factor(df$facet_lab,
                       levels = c(1,2,3,4),
                       labels = c("% Foreign-Born", 
                                  "% Non-EU Foreign-Born",
                                  "% from Arabic-Speaking Countries",
                                  "% Co-National"))



### add sig at .05

df$sig_05 <- ifelse(df$pv<0.05,1,0)
df$sig_05 <- as.factor(df$sig_05)

## plot
library(ggplot2)
cbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#000000")



#### Subset balance plot just to demographic variables
df_dem <- subset(df,df$Hyp=="Demographic")


png(".../2.Balance Checks/Output/D.Figures/BalancePlot_DemVars.png",
    width=10,height=6,units='in',res=300)
ggplot(data = df_dem, aes(x = pv, y = Variable,shape=sig_05)) + 
  facet_wrap(~facet_lab) +
  geom_vline(xintercept = 0.05, lty = 2) +
  geom_point(size = 2) +
  ylab(NULL) +
  xlab(expression(paste(italic("p"), "-value", sep=""))) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none",
        panel.grid.major.y = element_line(size = .15, linetype = 2, color = "gray75")) 
dev.off()


#### Subset balance plot just to demographic variables
df_other <- subset(df,df$Hyp!="Demographic")

png(".../2.Balance Checks/Output/D.Figures/BalancePlot_ControlVars.png",
    width=10,height=6,units='in',res=300)
ggplot(data = df_other, aes(x = pv, y = Variable,shape=sig_05)) + 
  facet_wrap(~Outcome) +
  geom_vline(xintercept = 0.05, lty = 2) +
  geom_point(size = 2) +
  ylab(NULL) +
  xlab(expression(paste(italic("p"), "-value", sep=""))) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none",
        panel.grid.major.y = element_line(size = .15, linetype = 2, color = "gray75")) 
dev.off()


### Demographic plot for non-COB Fe Model


rm(list=ls())

# read in frame
df <- read.csv(".../2.Balance Checks/Output/B.Tables/Combined_Table_withoutCOBFE.csv")


# pre-processing
df <- data.frame(df)

df$facet_lab <- ifelse(df$Outcome=="% Foreign-Born",1,
                       ifelse(df$Outcome=="% Non EU",2,
                              ifelse(df$Outcome=="% from Arabic-Speaking Countries",3,
                                     ifelse(df$Outcome=="% Co-National",4,NA))))



df$facet_lab <- factor(df$facet_lab,
                       levels = c(1,2,3,4),
                       labels = c("% Foreign-Born", 
                                  "% Non-EU Foreign-Born",
                                  "% from Arabic-Speaking Countries",
                                  "% Co-National"))


### add sig at .05

df$sig_05 <- ifelse(df$pv<0.05,1,0)
df$sig_05 <- as.factor(df$sig_05)

## plot
library(ggplot2)
cbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#000000")



#### Subset balance plot just to demographic variables
df_dem <- subset(df,df$Hyp=="Demographic")


png(".../2.Balance Checks/Output/D.Figures/BalancePlot_DemVars_noCoBFE.png",
    width=10,height=6,units='in',res=300)
ggplot(data = df_dem, aes(x = pv, y = Variable,shape=sig_05)) + 
  facet_wrap(~facet_lab) +
  geom_vline(xintercept = 0.05, lty = 2) +
  geom_point(size = 2) +
  ylab(NULL) +
  xlab(expression(paste(italic("p"), "-value", sep=""))) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none",
        panel.grid.major.y = element_line(size = .15, linetype = 2, color = "gray75")) 
dev.off()

#### Subset balance plot just to demographic variables
df_other <- subset(df,df$Hyp!="Demographic")

png(".../2.Balance Checks/Output/D.Figures/BalancePlot_ControlVars_noCoBFE.png",
    width=10,height=6,units='in',res=300)
ggplot(data = df_other, aes(x = pv, y = Variable,shape=sig_05)) + 
  facet_wrap(~Outcome) +
  geom_vline(xintercept = 0.05, lty = 2) +
  geom_point(size = 2) +
  ylab(NULL) +
  xlab(expression(paste(italic("p"), "-value", sep=""))) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none",
        panel.grid.major.y = element_line(size = .15, linetype = 2, color = "gray75")) 
dev.off()

